﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba
{
    public partial class yingjian : System.Web.UI.Page
    {
        sjklqDataContext sj = new sjklqDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                List<GetRootCategoriesResult> categories = sj.GetRootCategories().ToList();
                //循环遍历顶层节点列表
                foreach (GetRootCategoriesResult c in categories)
                {
                    //实例化TreeNode对象
                    TreeNode newNode = new TreeNode(c.title, c.id);
                    //在选择节点时，展开节点下的子节点
                    newNode.SelectAction = TreeNodeSelectAction.SelectExpand;
                    TreeView1.Nodes.Add(newNode);  //添加节点
                    populateCategoryNode(newNode); //递归添加子节点
                }
            }
        }

        private void populateCategoryNode(TreeNode n)
        {   //得到当前节点的子分类列表
            List<GetNonRootCategoriesResult> categories = sj.GetNonRootCategories(n.Value).ToList();
            //如果子分类总数为0，表示当前己经为根节点，无须再次遍历
            if (categories.Count() == 0)
            {
                return;             //递归的出口
            }
            else                    //循环节点列表，递归调用populateCategoryNode节点
            {
                foreach (var c in categories)
                {
                    //实例化节点对象
                    TreeNode newNode = new TreeNode(c.title, c.id);
                    //设置SelectAction
                    newNode.SelectAction = TreeNodeSelectAction.SelectExpand;
                    n.ChildNodes.Add(newNode);      //添加子节点
                    populateCategoryNode(newNode);  //递归调用自身
                }
            }
        }

        protected void LinkButton1_OnCommand(object sender, CommandEventArgs e)
        {

        }
    }
}